<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
define("DEBUG",false);
class excursion extends CI_Controller {

	public function index()
	{
		exit();
	}
	public function excurs()
	{
		$pto=(isset($_GET['pto'])?(int)$_GET['pto']:0);
		$agent=(isset($_GET['oto'])?(int)$_GET['oto']:0);
		$callback = (isset($_GET["callback"])?$_GET["callback"]:'');
		
		if($pto==0||$agent==0||$callback=='')exit();
		
		
		header('Access-Control-Allow-Origin: *');
		$this->output->set_header("HTTP/1.1 200 OK");
		//for IE
		if(!file_exists("cache/e".$pto.".json") or DEBUG)
		{
			$this->load->model('excursion_json','json');
			$cont = $this->json->getexcurslist($pto,$agent);
			$fh = fopen("cache/e".$pto.".json", "w");
			fwrite($fh,$cont);
			fclose($fh);
			echo $callback.'('.$cont.')';
		}
		else
		{
			//read the file
			$filename = "cache/e".$pto.".json";
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			echo $callback.'('.$contents.');';
		}
	}
	function excurs_full()
	{
		$id=(isset($_GET['id'])?(int)$_GET['id']:0);
		if($id==0)exit();
		$callback = (isset($_GET["callback"])?$_GET["callback"]:exit());
		
		header('Access-Control-Allow-Origin: *');
		//for IE
		if(!file_exists("cache/exc".$id.".json") or DEBUG)
		{
			$this->load->model('excursion_json','json');
			$cont = $this->json->full($id);
			$fh = fopen("cache/exc".$id.".json", "w");
			fwrite($fh,$cont);
			fclose($fh);
			echo $callback.'('.$cont.')';
		}
		else
		{
			//read the file
			$filename = "cache/exc".$id.".json";
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			echo $callback.'('.$contents.');';
		}
	}
	function showplaces()
	{
		$pto=(isset($_GET['pto'])?(int)$_GET['pto']:0);
		$agent=(isset($_GET['oto'])?(int)$_GET['oto']:0);
		$callback = (isset($_GET["callback"])?$_GET["callback"]:'');
		
		if($pto==0||$agent==0||$callback=='')exit();
		
		
		header('Access-Control-Allow-Origin: *');
		$this->output->set_header("HTTP/1.1 200 OK");
		//for IE
		
			$this->load->model('excursion_json','json');
			$cont = $this->json->getshowplacelist($pto,$agent);
			
			echo $callback.'('.$cont.')';
		
	
	}
	function price()
	{
		$id=(isset($_GET['pto'])?(int)$_GET['pto']:0);
		$date=(isset($_GET['date'])?$_GET['date']:'');
		$type=(isset($_GET['type'])?(int)$_GET['type']:0);
		$callback = (isset($_GET["callback"])?$_GET["callback"]:exit());
		
		if((!preg_match("/^(\d\d\d\d)\-(\d\d)\-(\d\d)/",$date))||$type==0||$callback==''||$id==0)
		die();
		
		header('Access-Control-Allow-Origin: *');
		//for IE
		$this->load->model('excursion_json','json');
		$cont = $this->json->price($id,$date,$type);
		echo $callback.'('.$cont.')';
		
	}
}